package com.example.librarysystem.Controller;

import com.example.librarysystem.Service.BookService;
import com.example.librarysystem.enums.BookStatusEnum;
import com.example.librarysystem.model.BookInfo;
import com.example.librarysystem.model.PageRequest;
import com.example.librarysystem.model.ResponseResult;
import io.micrometer.common.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;
    @RequestMapping("/addbook")
    public String addBook(BookInfo bookInfo) {
        if(bookInfo.getBookName()==null
                ||bookInfo.getAuthor()==null
                ||bookInfo.getPublish()==null
        ||bookInfo.getCount()==null
        ||bookInfo.getPrice()==null
        ||bookInfo.getStatus()==null) {
            return "参数不合法";
        }
        try{
            bookService.addBook(bookInfo);
            return "";
        }catch (Exception e){
            return "发生异常";
        }

    }
    @RequestMapping("/getList")
    public List<BookInfo> getList(){
        return bookService.getList();
    }
    @RequestMapping("/getListByPage")
    public ResponseResult<BookInfo> getListByPage(PageRequest pagerequest){
        return bookService.getListBypage(pagerequest);
    }
    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer id){
        return bookService.queryBookById(id);
    }
    @RequestMapping("/updateBook")
    public String updateBook(BookInfo bookInfo) {
        try{
            bookService.updateBook(bookInfo);
            return "";
        }catch (Exception e){
            return "修改图书发生异常";
        }
    }
    @RequestMapping("/deleteBook")
    public String deleteBook(Integer id) {
        try{
            BookInfo bookInfo = new BookInfo();
            bookInfo.setId(id);
            bookInfo.setStatus(BookStatusEnum.DELETED.getCode());
            bookService.updateBook(bookInfo);
            return "";
        }catch (Exception e){
            return "删除图书发生异常";
        }
    }
    @RequestMapping("/batchDelete")
    public Boolean batchDelete(@RequestParam List<Integer> ids) {
        try{
            bookService.batchDelete(ids);
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
